<!doctype html>
<title>load() fires abort/emptied events when networkState is not NETWORK_EMPTY</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/media.js"></script>
<div id=log></div>
<script>
function load_test(t, v) {
  assert_not_equals(v.networkState, v.NETWORK_EMPTY);

  var expected_events = [];
  if (v.networkState == v.NETWORK_LOADING || v.networkState == v.NETWORK_IDLE) {
    expected_events.push('abort');
  }
  if (v.networkState != v.NETWORK_EMPTY) {
    expected_events.push('emptied');
  }

  var actual_events = [];
  v.onabort = v.onemptied = t.step_func(function(e) {
    actual_events.push(e.type);
  });

  v.onloadstart = t.step_func(function() {
    assert_array_equals(actual_events, expected_events);
    t.done();
  });

  v.load();

  assert_array_equals(actual_events, [], 'events should be fired in queued tasks');
}

async_test(function(t) {
  var v = document.createElement('video');
  // suspend is fired optionally "if the user agent intends to not attempt to
  // fetch the resource" or "once the entire media resource has been fetched"
  v.preload = 'none';
  v.src = getAudioURI('/media/sound_5');
  v.onerror = t.unreached_func();
  v.onsuspend = t.step_func(function() {
    v.onsuspend = null;
    assert_equals(v.networkState, v.NETWORK_IDLE);
    load_test(t, v);
  });
}, 'NETWORK_IDLE');

async_test(function(t) {
  var v = document.createElement('video');
  v.src = 'resources/delayed-broken-video.py';
  v.onerror = t.unreached_func();
  v.onloadstart = t.step_func(function() {
    v.onloadstart = null;
    assert_equals(v.networkState, v.NETWORK_LOADING);
    load_test(t, v);
  });
}, 'NETWORK_LOADING');

async_test(function(t) {
  var v = document.createElement('video');
  v.src = 'data:,';
  v.onerror = t.step_func(function() {
    v.onerror = null;
    assert_equals(v.networkState, v.NETWORK_NO_SOURCE);
    load_test(t, v);
  });
  assert_equals(v.networkState, v.NETWORK_NO_SOURCE);
}, 'NETWORK_NO_SOURCE');
</script>
